//
//  InformationViewController.m
//  DianNaoWang
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CFWebViewController.h"
#import "DataBaseManager.h"
#import "Header.h"
@interface CFWebViewController ()<UIWebViewDelegate>
@property(nonatomic, assign)NSInteger flag;
@property(nonatomic, retain)UIWebView *webView;
@property(nonatomic, retain)UIAlertView *alertView;
@property(nonatomic, retain)UIAlertView *alertView2;
@end

@implementation CFWebViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

-(void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _flag = 0;
    [DataBaseManager creatTable];
    self.view.backgroundColor = BackgroundColor;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Nofavarater@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemAction)];
    //self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _activityView.center = self.view.center;
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_favorWap.wapUrlString]];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [_webView loadRequest:request];
    _webView.delegate = self;
    _webView.backgroundColor = BackgroundColor;
    [self.view addSubview:_webView];
    [self.view addSubview:_activityView];


#pragma mark 查询数据库是否存储本条,给收藏按钮赋颜色
    NSArray *array = [DataBaseManager selectWap];
    for (FavoriteWapPage *wapPage in array) {
        if ([wapPage.wapUrlString isEqualToString:_favorWap.wapUrlString]) {
            self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
            break;
        }
    }
    
    
}

- (void)barButtonItemAction
{
#pragma mark 运用数据库的取值存值实现收藏和是否收藏的判断
    NSMutableArray *array = [DataBaseManager selectWap];
    
    //看数据库里有没有值
    if (!array) {
        [DataBaseManager creatTable];
        [DataBaseManager insertFavoriteWapByFavoriteWapPage:_favorWap];
    }
    
    //创建数组存储网址(方便以后用containsObject)
    NSMutableArray *arr = [NSMutableArray array];
    for (FavoriteWapPage *wap in array) {
        [arr addObject:wap.wapUrlString];
    }
    
    //如果网址存在 提示已收藏过
    if ([arr containsObject:_favorWap.wapUrlString]) {
        ////NSLog(@"已收藏过");
        [DataBaseManager creatTable];
        [DataBaseManager deleteFavoriteWapByFavoriteWapPage: _favorWap];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
        self.alertView = [[UIAlertView alloc]initWithTitle:@"已取消收藏" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alertView show];
        [self performSelector:@selector(alertDismissAction) withObject:self afterDelay:.5];
        
    }else{
        [DataBaseManager creatTable];
        [DataBaseManager insertFavoriteWapByFavoriteWapPage:_favorWap];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
        ////NSLog(@"收藏成功");
        self.alertView = [[UIAlertView alloc]initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alertView show];
        [self performSelector:@selector(alertDismissAction) withObject:self afterDelay:.5];
    }

}

- (void)alertDismissAction
{
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}

-(void)setFavorWap:(FavoriteWapPage *)favorWap
{
    if (_favorWap != favorWap) {

        _favorWap = favorWap;
    }
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [_activityView startAnimating];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activityView stopAnimating];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
